package com.example.controller;


import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/getuser")
    @ResponseBody
    public List<User> getUsers() {
        return userMapper.selectList(null);
    }

    @GetMapping("/getuserbyid")
    @ResponseBody
    public User getUserById(@RequestParam("id") Long id) {
        return userMapper.selectById(id);
    }

    @PostMapping("/updateuser")
    @ResponseBody
    public Map<String, Object> updateUser(@RequestBody Map<String, String> request) {
        Map<String, Object> map = new HashMap<>();

        try {
            String id = request.get("id");
            String nickname = request.get("nickname");
            String avatar = request.get("avatar");

            if (id == null || nickname == null || avatar == null) {
                map.put("state", false);
                map.put("msg", "缺少必要的参数");
                return map;
            }

            User user = new User();
            user.setId(id); // 假设 User 类中的 id 是 String 类型
            user.setNickname(nickname);
            user.setAvatar(avatar);

            int result = userMapper.updateById(user);
            if (result > 0) {
                map.put("state", true);
                map.put("msg", "更新成功");
            } else {
                map.put("state", false);
                map.put("msg", "更新失败");
            }
        } catch (Exception e) {
            log.error("更新用户信息失败: {}", e.getMessage(), e);
            map.put("state", false);
            map.put("msg", e.getMessage());
        }

        return map;
    }

    @PostMapping("/updatepassword")
    @ResponseBody
    public Map<String, Object> updatePassword(@RequestBody Map<String, String> request) {
        Map<String, Object> map = new HashMap<>();

        try {
            String id = request.get("id");
            String newPassword = request.get("newPassword");

            if (id == null || newPassword == null) {
                map.put("state", false);
                map.put("msg", "缺少必要的参数");
                return map;
            }

            User user = new User();
            user.setId(id); // 假设 User 类中的 id 是 String 类型
            user.setPassword(newPassword);

            int result = userMapper.updateById(user);
            if (result > 0) {
                map.put("state", true);
                map.put("msg", "密码更新成功");
            } else {
                map.put("state", false);
                map.put("msg", "密码更新失败");
            }
        } catch (Exception e) {
            log.error("更新密码失败: {}", e.getMessage(), e);
            map.put("state", false);
            map.put("msg", e.getMessage());
        }

        return map;
    }

    @PostMapping("/upload")
    public String handleFileUpload(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                // 获取上传文件的名称
                String fileName = file.getOriginalFilename();

                // 创建目标文件存储路径
                File targetFile = new File
                        ("F:\\个人博客系统自制\\xjkBlog\\vue\\src\\assets\\upload\\" + fileName);

                // 将上传的文件保存到目标路径
                file.transferTo(targetFile);
                return "redirect:/success";
            } catch (IOException e) {
                log.error("文件上传失败", e);
                return "error";
            }
        } else {
            return "error";
        }
    }

    @GetMapping("/getToken")
    @ResponseBody
    public Map<String, Object> getToken() {
        Map<String, Object> map = new HashMap<>();

        try {
            Map<String, String> payload = new HashMap<>();
            String token = JWTUtils.getToken(payload);
            map.put("state", true);
            map.put("msg", "登录成功");
            map.put("token", token);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", e.getMessage());
            map.put("token", "");
        }
        return map;
    }

}
